package com.eachonline.smart.web.common;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
 
public class SmsUtil {
 
    public static void main(String[] args) {
    	
    	sendMsgToUser("13913889046", "123456");
    }
   
	
    /**
	    * 	0：成功
		* 	30：密码错误
		* 	40：账号不存在
		*   41：余额不足
		* 	42：帐号过期
		* 	43：IP地址限制
		* 	50：内容含有敏感词
		* 	51：手机号码不正确
	    * @param telphone
	    * @param code
	    */
public static String  sendCommonMsgToUser(String telphone,String code){
	
  String username = "yunmaserver"; //在短信宝注册的用户名
  String password = "zol!@123"; //在短信宝注册的密码
  String phone = telphone;
  String content = " 【九科学院】【九科学院】您的验证码为"+code+"，在5分钟内有效，您正在使用九科学院，需要校验。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

  String httpUrl = "http://api.smsbao.com/sms";

  StringBuffer httpArg = new StringBuffer();
  httpArg.append("u=").append(username).append("&");
  httpArg.append("p=").append(md5(password)).append("&");
  httpArg.append("m=").append(phone).append("&");
  httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));

  String result = request(httpUrl, httpArg.toString());
  
  System.out.println(result);
	return result;
}
    
    
    /**
	    * 	0：成功
		* 	30：密码错误
		* 	40：账号不存在
		*   41：余额不足
		* 	42：帐号过期
		* 	43：IP地址限制
		* 	50：内容含有敏感词
		* 	51：手机号码不正确
	    * @param telphone
	    * @param code
	    */
 public static String  sendMsgToUser(String telphone,String code){
 	
 	
     String username = "yunmaserver"; //在短信宝注册的用户名
     String password = "zol!@123"; //在短信宝注册的密码
     String phone = telphone;
     String content = "【九科学院】欢迎您注册九科学院，您的验证码为"+code+"，在5分钟内有效。"; // 注意测试时，也请带上公司简称或网站签名，发送正规内容短信。千万不要发送无意义的内容：例如 测一下、您好。否则可能会收不到

     String httpUrl = "http://api.smsbao.com/sms";

     StringBuffer httpArg = new StringBuffer();
     httpArg.append("u=").append(username).append("&");
     httpArg.append("p=").append(md5(password)).append("&");
     httpArg.append("m=").append(phone).append("&");
     httpArg.append("c=").append(encodeUrlString(content, "UTF-8"));

     String result = request(httpUrl, httpArg.toString());
     
     System.out.println(result);
 	return result;
 }
    public static String request(String httpUrl, String httpArg) {
        BufferedReader reader = null;
        String result = null;
        StringBuffer sbf = new StringBuffer();
        httpUrl = httpUrl + "?" + httpArg;
 
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            String strRead = reader.readLine();
            if (strRead != null) {
                sbf.append(strRead);
                while ((strRead = reader.readLine()) != null) {
                    sbf.append("\n");
                    sbf.append(strRead);
                }
            }
            reader.close();
            result = sbf.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
 
    public static String md5(String plainText) {
        StringBuffer buf = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(plainText.getBytes());
            byte b[] = md.digest();
            int i;
            buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return buf.toString();
    }
 
    public static String encodeUrlString(String str, String charset) {
        String strret = null;
        if (str == null)
            return str;
        try {
            strret = java.net.URLEncoder.encode(str, charset);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return strret;
    }
}